package com.oocl.euc.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class JacksonUtil {

    private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    private JacksonUtil() {
    }

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String objToJson(Object o) {
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            logger.error("JsonProcessingException",e);
        }
        return null;
    }

    public static <T> T jsonToObj(String jsonString, Class<T> destinationClass) {
        try {
            return objectMapper.readValue(jsonString, destinationClass);
        } catch (IOException e) {
            logger.error("IOException",e);
        }
        return null;
    }

}
